import * as types from './mainScreenActionTypes';

export function reduxSelectfarm(index) {
  return {
    type: types.SELECTFARM,
    index:index
  };
};

export function reduxChangeFarmunit(farmunitname) {
  return {
    type: types.CHANGEFARMUNIT,
    farmunitname:farmunitname
  };
};
//================================进入单个bwlist==================================
export function reduxEnterUnit(currentSec,currentAddr) {
  return {
    type: types.ENTER_FARM_UNIT,
    currentSec:currentSec,
    currentAddr:currentAddr
  };
};
//========================改变mongodb的ip和端口===================================
export function reduxchangeMongoIpPort(ip,port) {
  return {
    type: types.CHANG_MONGO_IP_PORT,
    ip:ip,
    port:port
  };
};
//action 开始fetch
export function requestPosts(){
  return{
    type:types.SET_FETCTH_STATE
  }
};
//====================action，fetch成功得到数据后更新state======================
function receiveManinscreenDataPosts(prarStructJson){
  return{
    type:types.UPDATA_MAINSCREEN,
    prarStructJson:prarStructJson
  }
};
// ========================如果超时或者其他fetch错误=========================
function fetchFaild(){
  return{
    type:types.FETCH_FAILD
  }
}
//================从mongodb拉取首屏用的渲染用的数据=============================
export function reduxFetchManinscreenData() {
  return dispatch => {
    dispatch(requestPosts());//开始
    return timeout(2000, fetch('http://10.33.51.107:28017/bw800golang/paraStruct/'))
      .then(response => response.json())
      .then((responseText) => {
        //如果拉取成功，就更新数据
        return dispatch(receiveManinscreenDataPosts(responseText.rows));
      })
      .catch(function(error) {
        // might be a timeout error
        // 如果超时或者其他fetch错误
        dispatch(fetchFaild())
      })
  }
};
//===========================拉取日下料记录成功后的处理=====================================
function receiveDayRecordData(dayRecordJson){
  return{
    type:types.UPDATA_DAYRECORDDATA,
    dayRecordJson:dayRecordJson
  }
}
//=============================从mongodb拉取日下料记录=====================================
export function reduxFetchDayRecordDataByAddr(addr) {
  //console.log(addr);
  var url='http://10.33.51.107:28017/bw800golang/dayRecord/';
  //s 是为了让get可以传string参数，看笔记  “简单的restful，http mongo”
  var url_filter=url+'?filter_Addr=s'+addr;//http://127.0.0.1:28017/bw800golang/dayRecord/?filter_Addr=s0000000000
  return dispatch => {
    dispatch(requestPosts());//开始
    return timeout(3000, fetch(url_filter))
      .then(response => response.json())
      .then((responseText) => {
        //如果拉取成功，就更新数据
        return dispatch(receiveDayRecordData(responseText.rows));
      })
      .catch(function(error) {
        // might be a timeout error
        // 如果超时或者其他fetch错误
        dispatch(fetchFaild())
      })
  }
};
//====================不设置为测试模式========================
export function reduxSetTestMode(){
  return{
    type:types.SET_TESTMODE
  }
};
//====================不设置为测试模式========================
export function reduxDontSetTestMode(){
  return{
    type:types.DONTSET_TESTMODE
  }
}
//===================================================
//功能函数
//=====================================================
//让fetch带超时功能
function timeout(ms, promise) {
  return new Promise(function(resolve, reject) {
    setTimeout(function() {
      reject(new Error("timeout"))
    }, ms)
    promise.then(resolve, reject)
  })
}
